﻿namespace Deppton.Mobile.Views
{
    using System;
    using System.Collections.Specialized;
    using System.Globalization;
    using Deppton.Model;
    using System.Collections.Generic;

    public partial class GpsQuestionForm : QuestionForm
    {
        private string value;
        private NamedList<KeyValuePair<string, string>> questionMetadata;

        public GpsQuestionForm()
        {
            this.InitializeComponent();
        }

        public override string Value
        {
            get
            {
                return this.value;
            }
            set
            {
                this.value = value;

                this.UpdateInformation();
            }
        }

        public NamedList<KeyValuePair<string, string>> QuesitonMetadata
        {
            set
            {
                this.questionMetadata = value;

                this.UpdateInformation();
            }
        }

        public override bool ControlsEnabled
        {
            get
            {
                return base.ControlsEnabled;
            }
            set
            {
                base.ControlsEnabled = value;

                this.AcquirePositionButton.Enabled = value;
            }
        }

        private void UpdateInformation()
        {
            // TODO: Move strings to resources.
            this.MetadataDescriptionLabel.Text = this.questionMetadata["description"].Value;
            double latitude;
            double longitude;
            try
            {
                string[] values = Utilities.UnescapeList(this.value);

                if (values.Length != 2)
                {
                    return;
                }

                latitude = Convert.ToDouble(values[0], CultureInfo.InvariantCulture);
                longitude = Convert.ToDouble(values[1], CultureInfo.InvariantCulture);
            }
            catch
            {
                this.AcquiredPositionLabel.Text = "No location has been acquired.";
                return;
            }

            try
            {
                if (Convert.ToBoolean(this.questionMetadata["showValidationStatus"], CultureInfo.InvariantCulture))
                {
                    double tolerance = Convert.ToDouble(this.questionMetadata["tolerance"], CultureInfo.InvariantCulture);
                    double expectedLatitude = Convert.ToDouble(this.questionMetadata["latitude"], CultureInfo.InvariantCulture);
                    double expectedLongitude = Convert.ToDouble(this.questionMetadata["longitude"], CultureInfo.InvariantCulture);

                    if (Utilities.IsWithinRange(latitude, longitude, expectedLatitude, expectedLongitude, tolerance))
                    {
                        this.AcquiredPositionLabel.Text = "A location has been acquired and it is valid.";
                    }
                    else
                    {
                        this.AcquiredPositionLabel.Text = "A location has been acquired, but it is not valid.";
                    }
                }
                else
                {
                    this.AcquiredPositionLabel.Text = "A location has been acquired.";
                }
            }
            catch
            {
                AcquiredPositionLabel.Text = "No location has been acquired.";
            }
        }

        private void AcquirePositionButtonClick(object sender, EventArgs e)
        {
            using (var gpsController = new GpsController())
            {
                var position = gpsController.CapturePosition();

                if (position != null)
                {
                    this.Value = Utilities.EscapeList(position.Latitude.ToString(CultureInfo.InvariantCulture), position.Longitude.ToString(CultureInfo.InvariantCulture));
                }
            }

            this.UpdateInformation();
        }
    }
}
